package cn.ncupyh.Test;

public class StringToMulToString {
    public static void main(String[] args) {
        /*将字符串A和字符串B相乘,再返回字符串*/
        String a = "12345";
        String b = "123";
        //将a和b转化为数字再相乘
//         法一
        Integer num1 = new Integer(a);
//        法二
        int num2 = Integer.parseInt(b);

//        int sum = num1 * num2;
//        System.out.println("结果为:" + sum);

//        法三:
        int inta = StringtoLong("12");
        int intb = StringtoLong("13");
        System.out.println(inta);
        System.out.println(intb);

//       相乘
        int sum = inta * intb;
        String res = String.valueOf(sum);
        System.out.println("结果为:" + res);
    }
    //将字符串转化为整数
public static int StringtoLong(String str ){
        char[] arr = str.toCharArray();//将字符串存到字符数组
    int sum =0;
    for (int i = 0 ;  i< arr.length;i++) {
        char c = arr[i];
        ////////////////千万注意::::次方不能写成--->10^2!!!!!!!!!!这是错误写法
        sum = (int) ((c-'0') + sum * Math.pow(10,i));
    }
    return sum;
}


//    public static int StringtoLong2(String str) {
//        //char[] arr = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};
//        char[] chars = str.toCharArray();
//        int[] ints = new int[10];
//        //将字符数组转化为数字数组
//        for (int i = 0;i<chars.length;i++) {
//            switch (chars[i]){
//                case '0':{ints[i] = 0;break;}
//                case '1':{ints[i] = 1;break;}
//                case '2':{ints[i] = 2;break;}
//                case '3':{ints[i] = 3;break;}
//                case '4':{ints[i] = 4;break;}
//                case '5':{ints[i] = 5;break;}
//                case '6':{ints[i] = 6;break;}
//                case '7':{ints[i] = 7;break;}
//                case '8':{ints[i] = 8;break;}
//                case '9':{ints[i] = 9;break;}
//                default:
//                {System.out.println("default!");break;}
//            }
//        }
//        //将数字数组转化为数字
//        int sum =0;
//        for (int i = 0 ; i < ints.length;i++) {
////            char c = ints[i];
//            sum = (int) (ints[i]+ sum * Math.pow(10,i));
//        }
//        return  sum;
//    }

}
